package com.lzx.iteam.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.ContactNameCardActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.net.EventMemberMsg;
import com.lzx.iteam.util.StringUtil;
import io.rong.imageloader.core.DisplayImageOptions;
import io.rong.imageloader.core.ImageLoader;
import io.rong.imageloader.core.listener.ImageLoadingListener;
import io.rong.imageloader.core.assist.ImageScaleType;
import io.rong.imageloader.core.listener.SimpleImageLoadingListener;
import io.rong.imageloader.core.display.FadeInBitmapDisplayer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by shijunting on 15-5-27.
 */
public class EventMemberAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<EventMemberMsg> list;
    private DisplayImageOptions options;
    private ImageLoadingListener animateFirstListener;
    protected ImageLoader imageLoader;

    public EventMemberAdapter(Context context, ArrayList<EventMemberMsg> list) {
        this.mContext = context;
        this.list = list;
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.default_cloud_image)
                .showImageForEmptyUri(R.drawable.default_cloud_image)
                .showImageOnFail(R.drawable.default_cloud_image)
                .resetViewBeforeLoading(true)
                .delayBeforeLoading(500)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        animateFirstListener = new AnimateFirstDisplayListener();
        imageLoader = ImageLoader.getInstance();
    }

    public void bindData(String text) {
        EventMemberMsg eventMemberMsg = new EventMemberMsg();
        eventMemberMsg.setUserName(text);
        list.add(eventMemberMsg);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final EventMemberMsg memberMsg = list.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.group_grid_item, null);
            holder.img = (ImageView) convertView.findViewById(R.id.iv_grid_manager_img);
            holder.name = (TextView) convertView.findViewById(R.id.tv_grid_manager_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(memberMsg.getUserName());
        if ("添加".equals(list.get(position).getUserName()) && position == list.size() - 1) {
            holder.img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.selector_manager_add));
        } else if ("暂无".equals(list.get(position).getUserName()) && position == list.size() - 1) {
            holder.img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.nobody_image));
        } else {
            imageLoader.displayImage(memberMsg.getUserImg(), holder.img, options, animateFirstListener);
            holder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if ("0".equals(memberMsg.getUserId()) || StringUtil.isEmpty(memberMsg.getUserId())) {
                        Toast.makeText(mContext, "对不起，该用户未注册", Toast.LENGTH_LONG).show();
                    } else {
                        Intent intent = new Intent(mContext, ContactNameCardActivity.class);
                        intent.putExtra("user_id", memberMsg.getUserId());
                        mContext.startActivity(intent);
                    }
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView img;
        TextView name;
    }

    private class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    //FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

}
